﻿using KTD.Web.BLL;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    /// <summary>
    /// 销售人员控制器
    /// </summary>
    public class SalerController : Controller
    {
        List<SelectListItem> statusList = new List<SelectListItem>() {
            new SelectListItem(){Text="审核中",Value="0"},
            new SelectListItem(){Text="未通过",Value="1"},
            new SelectListItem(){Text="审核通过",Value="2"}
        };
        // GET: Saler
        public ActionResult Index(SalerQD im)
        {
            if (im.Status == null)
            {
                im.Status = Models.Enum.Status.审核中;
            }
            im.PageSize = 10;
            var res = MF.SalerManager.GetListBy(im);
            ViewBag.Type = im.Type;
            ViewBag.statusList = statusList;
            res.ControllerContext = ControllerContext;
            res.Action = "Index";
            return View(res);
        }

        public ActionResult Detail(int Id)
        {
            ViewBag.StatusList = statusList;

            ViewBag.Id = Id;
            var res = MF.SalerManager.Find(Id);
            return View(res);
        }

        public ActionResult Update(int Id)
        {
            ViewBag.StatusList = statusList;
            ViewBag.houseId = MF.HouseManager.GetSelect();
            ViewBag.Id = Id;
            var res = MF.SalerManager.Find(Id);


            return View(res);
        }


        public ActionResult Create(string Type)
        {
            ViewBag.houseId = MF.HouseManager.GetSelect();
            ViewBag.StatusList = statusList;
            var model = new SalerCD();
            model.type = Type;
            return View(model);

        }


        [HttpPost]

        [ValidateInput(false)]
        public ActionResult Create(SalerCD im)
        {

            try
            {
                if (im.fileAvatar != null && im.fileAvatar.ContentLength > 0)
                {
                    im.avatar = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileAvatar.FileName}";
                    var combinePath = Server.MapPath($"~{im.avatar }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileAvatar.SaveAs(combinePath);
                }

                if (im.fileCode != null && im.fileCode.ContentLength > 0)
                {
                    im.code = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileCode.FileName}";
                    var combinePath = Server.MapPath($"~{im.code }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileCode.SaveAs(combinePath);
                }
                if (im.fileGp != null && im.fileGp.ContentLength > 0)
                {
                    im.gp = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileGp.FileName}";
                    var combinePath = Server.MapPath($"~{im.gp }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileGp.SaveAs(combinePath);
                }

                MF.SalerManager.Create(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                MF.SalerManager.Delete(Id);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }


        public ActionResult Find(int Id)
        {
            try
            {
                var res = MF.SalerManager.Find(Id);
               
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public ActionResult Update(SalerCD im)
        {
            try
            {
                if (im.fileAvatar != null && im.fileAvatar.ContentLength > 0)
                {
                    im.avatar = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileAvatar.FileName}";
                    var combinePath = Server.MapPath($"~{im.avatar }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileAvatar.SaveAs(combinePath);
                }



                if (im.fileCode != null && im.fileCode.ContentLength > 0)
                {
                    im.code = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileCode.FileName}";
                    var combinePath = Server.MapPath($"~{im.code }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileCode.SaveAs(combinePath);
                }
                if (im.fileGp != null && im.fileGp.ContentLength > 0)
                {
                    im.gp = $"/Attachment/{Guid.NewGuid().ToString()}/{im.fileGp.FileName}";
                    var combinePath = Server.MapPath($"~{im.gp }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    im.fileGp.SaveAs(combinePath);
                }


                MF.SalerManager.Update(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }


        [HttpPost]
        public ActionResult Like(SalerLikeD im)
        {
            try
            {
                MF.SalerManager.Like(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }
        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetList(SalerQD im)
        {
            try
            {
                var res = MF.SalerManager.GetListBy(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]

        [ValidateInput(false)]
        public ActionResult WxCreate(SalerCD im)
        {

            try
            {
                im.status = Models.Enum.Status.审核中;
                MF.SalerManager.Create(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}